﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using ClassLibrary1.Models;
using ClassLibrary1.Services;

namespace ClassLibrary1
{
    public class ClientRestHttpHandler : RestHttpHandler<Client, ClientModelBinder>
    {
        protected override IEnumerable<Client> GetAll()
        {
            return ClientService.GetAll();
        }
        protected override Client GetBy(int id)
        {
            return ClientService.GetById(id);
        }
        protected override IEnumerable<Client> GetBy(NameValueCollection query)
        {
            var result = ClientService.GetAll();
            var contains = query["contains"];
            if (contains != null)
            {
                result =
                    from item in result
                    where
                        item.FirstName.Contains(contains) ||
                        item.LastName.Contains(contains)
                    select item;
            }
            return result;
        }
        protected override void Create(Client entity)
        {
            ClientService.Create(entity);
        }
        protected override void Update(Client entity)
        {
            ClientService.Update(entity);
        }
        protected override void Delete(Client entity)
        {
            ClientService.Delete(entity);
        }
        protected override object ToJson(Client entity)
        {
            return new { entity.Id, entity.FirstName, entity.LastName };
        }
    }
}
